/**
 *	Read first!!!	
 *	This code uses this many Syscall because the sheet with this assigment said to do native system calls
 *	Do not remove 
 *
 *	Compile with gmsc Main.cs -unsafe -r:Mono.Posix.dll
 */
using System;
using System.IO;
using Mono.Unix.Native;

namespace xor_encryption
{
	unsafe class MainClass // using unsafe because of the intpointer used in the code
	{
		public static void Main(string[] args)
		{
        	string inFileName = args[0];
        	string outFileName = args[1];
			
	 		int inFile, outFile;
			int c=0;			
	
			if((inFile = Syscall.open(inFileName, OpenFlags.O_RDONLY|OpenFlags.O_CREAT, FilePermissions.ALLPERMS)) <0)
			{
				Console.WriteLine("Fail at opening input text file please check");
				Syscall.exit(1);
			}	
			
			if((outFile=Syscall.open(outFileName, OpenFlags.O_WRONLY|OpenFlags.O_CREAT, FilePermissions.ALLPERMS))<0)
			{
				Console.WriteLine("Fail at opening the output file, please check" );
				Syscall.exit(1);
			}
			
			
			while((Syscall.read(inFile, &c, 1)) >0)
			{
				c=c^31;
				Syscall.write(outFile,&c, 1);
			}
		 	
			//finally, close the files
			Syscall.close(inFile);
			Syscall.close(outFile);
			Syscall.chmod(outFileName, (FilePermissions.S_IRUSR|FilePermissions.S_IWUSR));//add chmod 600 ( == 400+200 )
			
			
			Console.WriteLine("All text succesfully encoded with XOR Encryption");		
		}
    }
}		